package control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Encoded;

import dao.UserData;

import model.User;
import nl.captcha.Captcha;

/**
 * Servlet implementation class FastRegistration
 */
public class FastRegistration extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FastRegistration() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String captcha = request.getParameter("captcha");
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		String pass1 = request.getParameter("pass1");
		String name = request.getParameter("name");
		String captcha_err = "";
		String email_err = "";
		String pass_err = "";
		String name_err = "";
		String endcode = Encoded.getSHA1(pass);
		UserData us = new UserData();
		boolean checkUser = us.checkEmail(email);
		Captcha cap = (Captcha) session.getAttribute(Captcha.NAME);
		System.out.println(cap+" cap");
		request.setCharacterEncoding("UTF-8");
		String answer = request.getParameter("answer");
		if (!cap.isCorrect(captcha)) {
			captcha_err = "Bạn nhập sai mã hình ảnh";
		}
		if(email.equals("")){
			email_err = "Bạn Chưa nhập tên tài khoản";
		}else if(checkUser){
			email_err = "Email này đã được sử dụng";
		}
		if(pass.equals("") || pass1.equals("")){
			pass_err = "Bạn chưa nhập mật khẩu";
		}
		if(!pass.equals(pass1)){
			pass_err = "Mật khẩu nhập lại không đúng";
		}
		if(!name.equals(name)){
			name_err = "Bạn chưa nhập họ tên";
		}
		if(!checkUser && pass.equals(pass1)&& cap.isCorrect(captcha) ){
			User user = new User(email, endcode, name, "", "", "", "", 0);
			us.addUser(user);
		}
		String url = "/congratulationReg.jsp";
		if (captcha_err.length() > 0|| pass_err.length() > 0||email_err.length() > 0||name_err.length()>0) {
			url = "/failReg.jsp";
			request.setAttribute("captcha_err", captcha_err);
			request.setAttribute("pass_err", pass_err);
			request.setAttribute("email_err", email_err);
			request.setAttribute("name_err", name_err);
		}
		RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
		rd.forward(request, response);

	}
}
